package fun.tan90.easy.binlog.manager.rest;

import fun.tan90.easy.binlog.base.convention.Res;
import fun.tan90.easy.binlog.base.convention.page.SimplePage;
import fun.tan90.easy.binlog.manager.model.cmd.BinlogMasterAddCmd;
import fun.tan90.easy.binlog.manager.model.cmd.BinlogMasterPageCmd;
import fun.tan90.easy.binlog.manager.model.cmd.BinlogMasterUpdateCmd;
import fun.tan90.easy.binlog.manager.model.vo.BinlogMasterInfoVo;
import fun.tan90.easy.binlog.manager.model.vo.BinlogMasterPageVo;
import fun.tan90.easy.binlog.manager.service.BinlogMasterService;
import fun.tan90.easy.binlog.manager.utils.DataSourceUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 控制层。
 *
 * @author 陈浩杰
 * @since 2023-09-01
 */
@RestController
@RequestMapping("/binlog-master")
public class BinlogMasterController {

    @Resource
    BinlogMasterService binlogMasterService;

    /**
     * 新增主节点
     *
     * @param addCmd
     * @return
     */
    @PostMapping
    public String addBinlogMaster(@RequestBody @Validated BinlogMasterAddCmd addCmd) {
        return binlogMasterService.addBinlogMaster(addCmd);
    }

    /**
     * 更新主节点
     *
     * @param updateCmd
     */
    @PutMapping
    public boolean updateBinlogMaster(@RequestBody @Validated BinlogMasterUpdateCmd updateCmd) {
        return binlogMasterService.updateBinlogMaster(updateCmd);
    }

    /**
     * 删除主节点
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("{id}")
    public boolean delBinlogMaster(@PathVariable String id) {
        return binlogMasterService.delBinlogMaster(id);
    }

    /**
     * 获取主节点基础信息
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("{id}")
    public BinlogMasterInfoVo info(@PathVariable String id) {
        return binlogMasterService.info(id);
    }

    /**
     * 主节点分页查询
     *
     * @param pageCmd
     * @return
     */
    @PostMapping("paging")
    public Res<SimplePage<BinlogMasterPageVo>> paging(@RequestBody BinlogMasterPageCmd pageCmd) {
        return binlogMasterService.paging(pageCmd);
    }

    /**
     * 启动slave客户端
     *
     * @param masterId
     */
    @GetMapping("publish")
    public String publishBinlogClient(@RequestParam String masterId) {
        return binlogMasterService.publishBinlogClient(masterId);
    }

    /**
     * 关闭slave客户端
     *
     * @param masterId
     */
    @GetMapping("close")
    public String closeBinlogClient(@RequestParam String masterId) {
        return binlogMasterService.closeBinlogClient(masterId);
    }

    /**
     * 主节点测试连接
     *
     * @param hostname
     * @param port
     * @param username
     * @param password
     * @return
     */
    @GetMapping("connection/test")
    public String checkMaster(String hostname, Integer port, String username, String password) {
        return DataSourceUtil.checkMaster(hostname, port, username, password);
    }
}
